package com.atguigu.tingshu.order.strategy.impl;
import java.math.BigDecimal;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.user.client.VipServiceConfigFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.google.common.collect.Lists;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * Author  王东剑
 * Created on 21/6/2024 PM6:40
 * Package_name com.atguigu.tingshu.order.strategy.impl
 * Description
 */
@Service
@Slf4j
public class VipOrderTradeStrategy implements OrderTradeStrategy {
	@Autowired
	private AlbumInfoFeignClient albumInfoFeignClient;
	@Autowired
	private UserInfoFeignClient userInfoFeignClient;
	@Autowired
	private VipServiceConfigFeignClient vipServiceConfigFeignClient;
	@Autowired
	private StringRedisTemplate redisTemplate;
	@Override
	public OrderInfoVo trade(TradeVo tradeVo) {
		Long itemId = tradeVo.getItemId();
		// 根据 vip服务配置Id 获取该 Vip服务配置信息
		VipServiceConfig vipServiceConfig = vipServiceConfigFeignClient.getVipServiceConfig(itemId).getData();
		// 构造返回对象
		OrderInfoVo orderInfoVo = new OrderInfoVo();

		// 原价
		BigDecimal originalAmount = vipServiceConfig.getPrice();
		// 折扣以后得金额
		BigDecimal discountPrice = vipServiceConfig.getDiscountPrice();
		// 折扣金额
		BigDecimal derateAmount = originalAmount.subtract(discountPrice);
		orderInfoVo.setOriginalAmount(discountPrice);
		orderInfoVo.setDerateAmount(derateAmount);
		orderInfoVo.setOrderAmount(discountPrice);

		// 设置订单明细信息
		OrderDetailVo orderDetailVo = new OrderDetailVo();
		orderDetailVo.setItemId(itemId);
		orderDetailVo.setItemName(vipServiceConfig.getName());
		orderDetailVo.setItemUrl(vipServiceConfig.getImageUrl());
		orderDetailVo.setItemPrice(originalAmount);
		orderInfoVo.setOrderDetailVoList(List.of(orderDetailVo));
		// 设置折扣信息
		OrderDerateVo orderDerateVo = new OrderDerateVo();
		orderDerateVo.setDerateType("1406");
		orderDerateVo.setDerateAmount(derateAmount);
		orderDerateVo.setRemarks("VIP折扣");
		orderInfoVo.setOrderDerateVoList(List.of(orderDerateVo));
		// 设置其她信息
		this.setOtherParameters(orderInfoVo,redisTemplate);
		// 返回数据
		return orderInfoVo;
	}

	@Override
	public String getItemType() {
		return SystemConstant.ORDER_ITEM_TYPE_VIP;
	}
}
